package com.emmy.walnut.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.emmy.walnut.R;
import com.emmy.walnut.constant.AppConstant;
import com.emmy.walnut.presenter.LoginPresenter;
import com.emmy.walnut.presenter.iface.LoginPIface;
import com.emmy.walnut.util.PreferenceUtil;
import com.emmy.walnut.view.iface.LoginIface;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

public class LoginActivity extends BaseActivity implements LoginIface {

    private LoginPIface loginPIface;
    private QMUIRoundButton login;
    private QMUIRoundButton register;

    private EditText username;
    private EditText password;

    private String strUsername;
    private String strPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        init();
    }

    /**
     *
     */
    private void init(){
        Log.d("TAG", Environment.getExternalStorageDirectory().getPath());
        loginPIface = new LoginPresenter(this);

        if (PreferenceUtil.getBoolean("isLogin")){
            loginPIface.loginWithoutAuthen();
/*            AppConstant.uuid = PreferenceUtil.getString("uuid");
            AppConstant.accessKey = PreferenceUtil.getString("access_key");
            loginPIface.tradeIn(AppConstant.uuid, AppConstant.accessKey);*/
        }

        login = findViewById(R.id.login);
        register = findViewById(R.id.register);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                strUsername = username.getText().toString();
                strPassword = password.getText().toString();

                if (strUsername.equals(""))
                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                else if(strPassword.equals(""))
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                else
                    loginPIface.login(strUsername, strPassword);
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });

    }


    @Override
    public void loginSuccess() {
        Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
    }

    @Override
    public void loginFailed(String msg) {
        Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginWithoutAuth() {
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
    }

    @Override
    public void reLogin() {
        Toast.makeText(this, "登陆已过期，请重新登陆", Toast.LENGTH_SHORT).show();
        PreferenceUtil.clear();
    }

    @Override
    public void continueLogin(String uuid, String accessKey) {
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        PreferenceUtil.putStringWithApply("access_key", accessKey);
    }
}
